//
//  ARShapeLayer.swift
//  ARScanning
//
//  Created by wanglc on 2018/3/7.
//  Copyright © 2018年 GitHub. All rights reserved.
//

import UIKit

struct ArcShape {
    
    var radius: CGFloat
    var lineWidth: CGFloat
    var opacity: Float
    
    var fillColor: UIColor
    var strokeColor: UIColor
}

struct ArcDegree {
    
    var fromDegree: CGFloat
    var toDegree: CGFloat
}

struct TriangleShape {
    
    var radius: CGFloat     //三角形外接圆半径
    var lineWidth: CGFloat
    var opacity: Float
    
    var fillColor: UIColor
    var strokeColor: UIColor
}

struct TrianglePosition {
    
    var centerPoint: CGPoint
    var vertex1: CGFloat    //三角形顶点角度
    var vertex2: CGFloat
    var vertex3: CGFloat
}

class ARShapeLayer: CAShapeLayer {

    //绘制弧形
    class func addArcLayerAt(center: CGPoint, degree: ArcDegree, shape: ArcShape) -> CAShapeLayer {
        
        let shapeLayer = CAShapeLayer()
        
        let arcPath = UIBezierPath(arcCenter: center,
                                   radius: shape.radius,
                                   startAngle: degree.fromDegree.toAngle(),
                                   endAngle: degree.toDegree.toAngle(),
                                   clockwise: true)
        
        shapeLayer.path = arcPath.cgPath
        shapeLayer.lineWidth = shape.lineWidth
        shapeLayer.lineCap = "round"
        shapeLayer.lineJoin = "round"
        shapeLayer.fillColor = shape.fillColor.cgColor
        shapeLayer.strokeColor = shape.strokeColor.cgColor
        shapeLayer.opacity = shape.opacity
//        shapeLayer.shouldRasterize = true       //设置缓存
//        shapeLayer.rasterizationScale = UIScreen.main.scale     //抗锯齿边缘
        
        return shapeLayer
    }
    
    //绘制三角形
    class func addTriangleLayer(position: TrianglePosition, shape: TriangleShape) -> CAShapeLayer {
        
        let shapeLayer = CAShapeLayer()
        
        let bezierPath = UIBezierPath()
        bezierPath.move(to: getPointAtCircle(centerPoint: position.centerPoint, radius: shape.radius, angle: position.vertex1))
        bezierPath.addLine(to: getPointAtCircle(centerPoint: position.centerPoint, radius: shape.radius, angle: position.vertex2))
        bezierPath.addLine(to: getPointAtCircle(centerPoint: position.centerPoint, radius: shape.radius, angle: position.vertex3))
        bezierPath.close()
        
        shapeLayer.path = bezierPath.cgPath
        shapeLayer.lineWidth = shape.lineWidth
        shapeLayer.lineCap = "round"
        shapeLayer.lineJoin = "round"
        shapeLayer.strokeColor = shape.fillColor.cgColor
        shapeLayer.fillColor = shape.strokeColor.cgColor
        shapeLayer.rasterizationScale = UIScreen.main.scale     //抗锯齿边缘
        
        return shapeLayer
    }
    
    //绘制折线
    class func addPolylineLayer(startPoint: CGPoint, otherPoints: Array<CGPoint>) -> CAShapeLayer {
        
        let shapeLayer = CAShapeLayer()
        shapeLayer.strokeStart = 0.0
        shapeLayer.strokeEnd = 0.0
        
        let bezierPath = UIBezierPath()
        bezierPath.move(to: startPoint)
        for point in otherPoints {
            bezierPath.addLine(to: point)
        }
        
        shapeLayer.path = bezierPath.cgPath
        shapeLayer.lineWidth = 1.0
        shapeLayer.lineCap = "round"
        shapeLayer.lineJoin = "round"
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = UIColor.white.cgColor
        shapeLayer.opacity = 0.4
        
        return shapeLayer
    }
    
    //绘制圆圈
    class func addCircleLayer(centerPoint: CGPoint, radius: CGFloat, opacity: Float, lineWidth: CGFloat, isFill: Bool) -> CAShapeLayer {
        
        var fillColor = UIColor.clear
        if isFill {
            fillColor = UIColor.white
        }
        
        let degree = ArcDegree(fromDegree: 0, toDegree: 360)
        let arcShape = ArcShape(radius: radius,
                                lineWidth: lineWidth,
                                opacity: opacity,
                                fillColor: fillColor,
                                strokeColor: UIColor.white)
        
        let shapeLayer = ARShapeLayer.addArcLayerAt(center: centerPoint,
                                                    degree: degree,
                                                    shape: arcShape)
        
        return shapeLayer
    }
    
    //添加渐变色蒙板
    class func addGradientLayerTo(layer: CALayer, msakLayer: CALayer, colors: Array<Any>) -> Void {
        
        let gradientLayer = CAGradientLayer()
        
        gradientLayer.frame = layer.bounds
        gradientLayer.colors = colors
        gradientLayer.startPoint = CGPoint(x: 1, y: 1)
        gradientLayer.endPoint = CGPoint(x: 0, y: 0)
        
        layer.addSublayer(gradientLayer)
        gradientLayer.mask = msakLayer
    }
    
    //绘制文字
    class func addTextLayer(frame: CGRect, text: String, font: UIFont) -> CATextLayer {
        
        let textLayer = CATextLayer()
        textLayer.frame = frame
        textLayer.contentsScale = UIScreen.main.scale
        textLayer.alignmentMode = kCAAlignmentJustified
        textLayer.isWrapped = true
        
        textLayer.font = font.fontName as CFTypeRef
        textLayer.fontSize = font.pointSize
        textLayer.string = text
        
        return textLayer
    }
}

extension CGFloat {
    
    //角度转换为弧度
    func toAngle() -> CGFloat {
        return .pi*self/180.0
    }
}

/// 获取圆上点的坐标
///
/// - Parameters:
///   - centerPoint: 圆心(a, b)
///   - radius: 半径r
///   - angle: 顶点角度m
/// - Returns: 顶点数组
/// 顶点坐标计算公式：
/// (r*cos(m*.pi/180)+a, r*sin(m*.pi/180) +b)
func getPointAtCircle(centerPoint: CGPoint, radius: CGFloat, angle: CGFloat) -> CGPoint {
    
    let pointX = radius * cos(angle * .pi/180) + centerPoint.x
    let pointY = radius * sin(angle * .pi/180) + centerPoint.y
    
    return CGPoint.init(x: pointX, y: pointY)
}
